package com.hyopt.modules.app.controller;

import com.hyopt.common.log.LogEnum;
import com.hyopt.common.log.LogUtils;
import com.hyopt.common.utils.Constant;
import com.hyopt.common.utils.DateUtils;
import com.hyopt.common.utils.FileUtils;
import com.hyopt.common.utils.R;
import com.hyopt.common.validator.ValidatorUtils;
import com.hyopt.common.validator.group.UpdateGroup;
import com.hyopt.modules.app.entity.ProjectInfo;
import com.hyopt.modules.app.form.ProjectInfoEditForm;
import com.hyopt.modules.app.service.ProjectInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author ：Li9527
 * @date ：Created in 2020/1/3
 * @description：项目配置控制类
 * @modified By：
 * @version: 1.0.0
 */
@RestController
@RequestMapping("/app/projectInfo")
@Api(value="AppProjectInfoController",tags={"项目配置管理"})
@Slf4j
public class AppProjectInfoController {
    Logger PROJECT_LOGS = LogUtils.Logger(LogEnum.PROJECT_LOGS);

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 获取项目信息列表
     * @return
     */
    @GetMapping("/List")
    @ApiOperation("获取项目信息列表")
    public R List(HttpServletRequest request) {
        PROJECT_LOGS.info("存在ip---" +  request.getRequestURI());
        List<ProjectInfo> list = projectInfoService.getListByOtherDb();
        if(list.size() >= 0){
            Iterator<ProjectInfo> it = list.iterator();
            while(it.hasNext()){
                ProjectInfo info = it.next();
                // 删除平台信息，只保留项目信息，因为启动前需要获取列表拿到启动图
                if(info.getId() == 1){
                    it.remove();
                }
            }
            return R.ok().put("data",list);
        }else{
            return R.error("请配置项目信息");
        }
    }

    /**
     * 获取该项目配置详情,如果本项目详情配置不存在，从基本服务项目库中同步
     * @return
     */
    @GetMapping("/info/{id}")
    @ApiOperation("获取项目信息列表")
    public R getInfoById(@PathVariable("id") @ApiParam(value = "项目id") Long id) {
        // 如果id等于1，从基本库取
        if(id == 1){
            ProjectInfo basicInfo = projectInfoService.getObjectByOtherDb(id);
            return R.ok().put("data", basicInfo);
        }else{
            ProjectInfo projectInfo = projectInfoService.getById(id);
            if (projectInfo != null) {
                return R.ok().put("data", projectInfo);
            }else {
                return R.error("基本配置读取失败");
            }
        }
    }


    @ApiOperation("配置编辑")
    @PostMapping("/update")
    public R update(@RequestBody @Validated(UpdateGroup.class) ProjectInfoEditForm form) {
        ValidatorUtils.validateEntity(form);
        ProjectInfo projectInfo = projectInfoService.getById(form.getId());
        if(projectInfo == null){
            return R.error("id传入错误");
        }

        if(form.getErrorrange()==0){
            form.setErrorrange(projectInfo.getErrorrange());
        }

        if(form.getAlarmhandlenumber()==0){
            form.setAlarmhandlenumber(projectInfo.getAlarmhandlenumber());
        }

        BeanUtils.copyProperties(form,projectInfo);

        if(projectInfoService.updateById(projectInfo)){
            return R.ok("编辑成功");
        }else{
            return R.error("编辑失败");
        }
    }

    /**
     * 上传图片--配置模块
     *
     * @param request
     * @return
     */
    @ApiOperation("上传图片")
    @PostMapping("upload/img")
    public R  updateImg(HttpServletRequest request) {
        try {
        String path = Constant.SYS_PUT + DateUtils.format(new Date(),DateUtils.DATE_PATTERN) + "/";
        String img = FileUtils.springUpload(request,path);
        return R.ok().put("data","/img/sys/" + DateUtils.format(new Date(),DateUtils.DATE_PATTERN) + "/" + img);
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }


}
